package com.boot.ys.mybatisplus.redis;

import com.alibaba.fastjson.JSONObject;
import com.boot.ys.mybatisplus.entity.Abc;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.sql.SQLOutput;
import java.util.*;
import java.util.stream.Collectors;

public class TestRedis {
    Jedis jedis = new Jedis("120.76.40.143");

    @Test
    public void testStringRedis(){
        Jedis jedis = new Jedis("120.76.40.143");
        jedis.set("runoobkey","www.runoob.com");
        System.out.println("redis 存储的字符串为:"+jedis.get("runoobkey"));
    }

    @Test
    public void testListRedis(){
        Jedis jedis = new Jedis("120.76.40.143");
        //存储数据到列表中
        jedis.lpush("site-list", "Runoob");
        jedis.lpush("site-list", "Google");
        jedis.lpush("site-list", "Taobao");
        List<String> list = jedis.lrange("site-list", 0 ,10);
        for(int i=0; i<list.size(); i++) {
            System.out.println("列表项为: "+list.get(i));
        }
    }

    @Test
    public void testListKeyRedis() {
        Jedis jedis = new Jedis("120.76.40.143");
        //存储数据到列表中
        Set<String> keys = jedis.keys("*");
        Iterator<String> it=keys.iterator() ;
        while(it.hasNext()){
            String key = it.next();
            System.out.println(key);
        }
    }

    @Test
    public void setTest() {
        jedis.del("set");
        jedis.sadd("set", "one", "two", "one", "three","Five","sex","Name");
        for(String element: jedis.smembers("set"))
            System.out.println(element);
    }

    @Test
    public void zsetTest() {
        jedis.del("zset");
        jedis.zadd("zset", 1000.0, "one");
        jedis.zadd("zset", 1000.0, "two");
        for(String key: jedis.zrange("zset", 0, -1))
            System.out.println(key);
    }


    @Test
    public void zsetTest2() {
        jedis.del("zset2");
        HashMap<String, Double> map = new HashMap<String, Double>();
        map.put("Tom", 200.0);
        map.put("Mike", 300.0);
        jedis.zadd("zset2", map);
        for (String name: jedis.zrange("zset2", 0, -1))
            System.out.println(name);
    }


    @Test
    public void TestForeach(){

        ArrayList<Abc> list = new ArrayList<>();

        Abc abc1 = new Abc();
        abc1.setId(1);
        abc1.setShuoming("测试1");
        abc1.setName("名字1");
        abc1.setTesta("String1");

        Abc abc2 = new Abc();
        abc2.setId(2);
        abc2.setShuoming("测试2");
        abc2.setName("名字2");
        abc2.setTesta("String2");

        Abc abc3 = new Abc();
        abc3.setId(3);
        abc3.setShuoming("测试3");
        abc3.setName("名字3");
        abc3.setTesta("String3");


        list.add(abc1);
        list.add(abc2);
        list.add(abc3);


//        list.forEach(c->{
//            System.out.println(c);
//        });

        List<Object> collect = list.stream().map(c -> {
            c.setName("11");
            System.out.println(c);
            return c;
        }).collect(Collectors.toList());
    }

    @Test
    public void test(){
        long l = System.currentTimeMillis();
        int i = new Random().nextInt(100);
        System.out.println(l);
        System.out.println(i);
    }

    @Test
    public void teJson(){
        JSONObject json1 = new JSONObject();
        json1.put("q","q1");
        json1.put("q3","q3");
        json1.put("q2","q2");

        JSONObject json = new JSONObject();
        json.put("a","ak");
        json.put("b","bk");
        json.put("c",json1);

        System.out.println(json.get("c"));

    }



}
